#ifndef _MAIN_H_
#define _MAIN_H_

#include "tx_api.h"

#define DEVICE_ID 3  //>=3

typedef enum { Success = 0, Failure = 1 } Function_State_Typedef;

#pragma pack(1)

struct fault_code1_bit {
    unsigned char pd1 : 1;
    unsigned char pd2 : 1;
    unsigned char pd3 : 1;
    unsigned char t1 : 1;
    unsigned char t2 : 1;
    unsigned char t3 : 1;
    unsigned char t4 : 1;
    unsigned char brake_state : 1;
};

union fault_code1_all {
    volatile unsigned char all;
    struct fault_code1_bit bit;
};

struct fault_code2_bit {
    unsigned char : 1;
    unsigned char seedSrcErr : 1;
    unsigned char humidity_err : 1;
    unsigned char envi_err : 1;
    unsigned char flow_err : 1;
    unsigned char laser_comm_err : 1;
    unsigned char power_comm_err : 1;
    unsigned char rsvd_0 : 1;
};

union fault_code2_all {
    volatile unsigned char all;
    struct fault_code2_bit bit;
};

struct fault_code3_bit {
    unsigned char output_seq_err : 1;
    unsigned char qbh_err : 1;
    unsigned char : 1;
    unsigned char : 1;
    unsigned char rsvd_0 : 4;
};

union fault_code3_all {
    volatile unsigned char all;
    struct fault_code3_bit bit;
};

struct power_operating_paramter {
    unsigned char power_state;     // 驱动源状态
    unsigned char modul1_V;        // 预放1输出电压
    unsigned char modul1_C_Out;    // 预放1输出电流
    unsigned char modul1_C_Set;    // 预放1设置电流
    unsigned char modul1_State;    // 预放1状态
    unsigned char modul2_V;        // 预放2输出电压
    unsigned char modul2_C_Out;    // 预放2输出电流
    unsigned char modul2_C_Set;    // 预放2设置电流
    unsigned char modul2_State;    // 预放2状态
    unsigned char modul3_V;        // 主放1输出电压
    unsigned char modul3_C_Out_H;  // 主放1输出电流
    unsigned char modul3_C_Out_L;  // 主放1输出电流
    unsigned char modul3_C_Set_H;  // 主放1设置电流
    unsigned char modul3_C_Set_L;  // 主放1设置电流
    unsigned char modul3_State;    // 主放1状态
    unsigned char modul4_V;        // 主放2输出电压
    unsigned char modul4_C_Out_H;  // 主放2输出电流
    unsigned char modul4_C_Out_L;  // 主放2输出电流
    unsigned char modul4_C_Set_H;  // 主放2设置电流
    unsigned char modul4_C_Set_L;  // 主放2设置电流
    unsigned char modul4_State;    // 主放2状态
    unsigned char version;         // 版本
};
struct report_operating_paramter {
    unsigned char power_state;   // 驱动源状态
    unsigned char modul1_C_Out;  // 预放1输出电流
    unsigned char modul1_V;      // 预放1输出电压
    // unsigned char modul1_C_Set;   // 预放1设置电流
    unsigned char modul1_State;  // 预放1状态
    unsigned char modul2_C_Out;  // 预放2输出电流
    unsigned char modul2_V;      // 预放2输出电压
    // unsigned char modul2_C_Set;   // 预放2设置电流
    unsigned char modul2_State;    // 预放2状态
    unsigned char modul3_C_Out_L;  // 主放1输出电流
    unsigned char modul3_C_Out_H;  // 主放1输出电流
    unsigned char modul3_V;        // 主放1输出电压
    // unsigned char modul3_C_Set_H; // 主放1设置电流
    // unsigned char modul3_C_Set_L; // 主放1设置电流
    unsigned char modul3_State;    // 主放1状态
    unsigned char modul4_C_Out_L;  // 主放2输出电流
    unsigned char modul4_C_Out_H;  // 主放2输出电流
    unsigned char modul4_V;        // 主放2输出电压
    // unsigned char modul4_C_Set_H; // 主放2设置电流
    // unsigned char modul4_C_Set_L; // 主放2设置电流
    unsigned char modul4_State;  // 主放2状态
};

struct laser_operating_parameter {
    uint8_t       Laser_State;  // 锟斤拷锟斤拷状态 0:锟斤拷锟斤拷 1:锟斤拷强锟斤拷 3:锟斤拷1锟斤拷锟斤拷锟斤拷 5:锟斤拷2锟斤拷锟斤拷锟斤拷
    uint8_t       PD1_Value;    // 前锟斤拷PD锟斤拷锟斤拷
    uint8_t       PD2_Value;    // 前锟斤拷PD锟斤拷锟斤拷
    uint8_t       PD3_Value;    // 前锟斤拷PD锟斤拷锟斤拷
    short         T1_Value;     // T1锟斤拷锟斤拷
    short         T2_Value;     // T2锟斤拷锟斤拷
    short         T3_Value;     // T3锟斤拷锟斤拷
    short         T4_Value;     // T4锟斤拷锟斤拷
    unsigned char EH_Value;     // 锟斤拷锟斤拷湿锟斤拷
    short         ET_Value;     // 锟斤拷锟斤拷锟铰讹拷

    unsigned char light_intensity;  // 锟斤拷强
    unsigned char standby;  // bit0:0锟斤拷锟斤拷锟斤拷模式 1锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷  bit1锟斤拷0锟斤拷未锟斤拷锟斤拷 1锟斤拷锟窖筹拷锟斤拷  bit2锟斤拷0:锟斤拷锟斤拷锟� 1锟斤拷锟饺达拷锟斤拷
    union fault_code1_all fault_code1;  // 锟斤拷锟斤拷锟斤拷1
    union fault_code2_all fault_code2;  // 锟斤拷锟斤拷锟斤拷2
    union fault_code3_all fault_code3;  // 锟斤拷锟斤拷锟斤拷3
};
struct seedSrc_parameter {
    uint8_t MOD;        // 前锟斤拷PD锟斤拷锟斤拷
    uint8_t MT;         // 前锟斤拷PD锟斤拷锟斤拷
    uint8_t AST;        // 前锟斤拷PD锟斤拷锟斤拷
    uint8_t seedState;  //
    uint8_t echo;       // 前锟斤拷PD锟斤拷锟斤拷
};

struct operating_state {
    struct power_operating_paramter  power_operate_parm;
    struct laser_operating_parameter laser_operate_data;
    struct seedSrc_parameter         seedSrc_operate_data;
};  // 锟斤拷态锟斤拷锟斤拷
struct reportOperating_state {
    struct report_operating_paramter power_operate_parm;
    struct laser_operating_parameter laser_operate_data;
    struct seedSrc_parameter         seedSrc_operate_data;
};  // 锟斤拷态锟斤拷锟斤拷

extern struct operating_state         operate_parm;
extern struct power_operating_current operateCuttent;

struct power_output_adaptor_paramter {
    unsigned char level1_modul1;    // 1级弱光 模块1电流
    unsigned char level1_modul2;    // 1级弱光 模块2电流
    unsigned char level1_modul3_L;  // 1级弱光 模块3电流低8位
    unsigned char level1_modul3_H;  // 1级弱光 模块3电流高8位
    unsigned char level1_modul4_L;  // 1级弱光 模块4电流低8位
    unsigned char level1_modul4_H;  // 1级弱光 模块4电流高8位

    unsigned char level2_modul1;    // 2级弱光 模块1电流
    unsigned char level2_modul2;    // 2级弱光 模块2电流
    unsigned char level2_modul3_L;  // 2级弱光 模块3电流低8位
    unsigned char level2_modul3_H;  // 2级弱光 模块3电流高8位
    unsigned char level2_modul4_L;  // 2级弱光 模块4电流低8位
    unsigned char level2_modul4_H;  // 2级弱光 模块4电流高8位

    unsigned char strong_modul1;    // 强光 模块1电流
    unsigned char strong_modul2;    // 强光 模块2电流
    unsigned char strong_modul3_L;  // 强光 模块3电流低8位
    unsigned char strong_modul3_H;  // 强光 模块3电流高8位
    unsigned char strong_modul4_L;  // 强光 模块4电流低8位
    unsigned char strong_modul4_H;  // 强光 模块4电流高8位

    unsigned short level1PreCur_up_limit;  // 预放1电流上限
    unsigned short level2PreCur_up_limit;  // 预放2电流上限
    unsigned short fa_up_limit;            // 主放电流上限
};

struct check_feature_1_part {
    unsigned char brake : 1;
    unsigned char power_comm : 1;
    unsigned char t1 : 1;
    unsigned char t2 : 1;
    unsigned char t3 : 1;
    unsigned char t4 : 1;
    unsigned char pd1 : 1;
    unsigned char pd2 : 1;
};

union check_feature_1_all {
    volatile unsigned char      all;
    struct check_feature_1_part part;
};

struct check_feature_2_part {
    unsigned char pd3 : 1;
    unsigned char env_temp : 1;
    unsigned char env_hum : 1;
    unsigned char mo : 1;
    unsigned char fa : 1;
    unsigned char reserve : 1;
    unsigned char reserve0 : 1;
    unsigned char reserve1 : 1;
};

union check_feature_2_all {
    volatile unsigned char      all;
    struct check_feature_2_part part;
};

struct laser_module_adaptor_paramter {
    unsigned char             can_id;
    union check_feature_1_all check_feature_1;
    union check_feature_2_all check_feature_2;
    unsigned char             pd1_low_limit;
    unsigned char             pd2_low_limit;
    unsigned char             pd3_low_limit;
    short                     t1_up_limit;
    short                     t1_low_limit;
    short                     t2_up_limit;
    short                     t2_low_limit;
    short                     t3_up_limit;
    short                     t3_low_limit;
    short                     t4_up_limit;
    short                     t4_low_limit;
    short                     env_temp_up_limit;
    short                     env_temp_low_limit;
    unsigned char             env_hum_up_limit;
    unsigned char             serialNM[4];
};

struct adaptor_paramter_bit {
    unsigned char                        BCP_Data;
    struct power_output_adaptor_paramter power_output_adaptor_parm;
    struct laser_module_adaptor_paramter laser_module_adaptor_parm;
};

#define ADA_PARM_64BIT_LEN (sizeof(struct adaptor_paramter_bit) / 8 + ((sizeof(struct adaptor_paramter_bit) % 8) ? (1) : (0)))

union adaptor_paramter_all {
    volatile unsigned long long all[ADA_PARM_64BIT_LEN];
    struct adaptor_paramter_bit part;
};

extern union adaptor_paramter_all adaptor_parm;

struct configurable_adaptor_paramter_avaliable {
    struct power_output_adaptor_paramter power_parm;
    struct laser_module_adaptor_paramter laser_parm;
};

struct configurable_adaptor_paramter_all {
    struct configurable_adaptor_paramter_avaliable avaliable;
    unsigned char                                  rsvd[64];
};

#pragma pack()

extern struct configurable_adaptor_paramter_all configurable_ada_parm;
extern union adaptor_paramter_all               flash_ada_parm;
extern struct reportOperating_state             operate_parm_report;

extern unsigned int pump_id_mark[4];

void                   delay_time(unsigned int times);
void                   memory_set(void *dst, unsigned char data, unsigned int size);
void                   memory_copy(void *dst, void *src, unsigned int len);
void                   clear_adaptor_rsvd(union adaptor_paramter_all *adaptor);
void                   init_ada_parm_and_store(union adaptor_paramter_all *p);
Function_State_Typedef Delay_1ms(volatile unsigned int i);
void                   delay_time(volatile unsigned int times);
ULONG                  time_span(ULONG start_time);
Function_State_Typedef Delay_1ms(volatile unsigned int i);

#endif
